from datetime import datetime, timedelta
import calendar

def get_month_range(date):
    """
    获取指定日期所在月份的第一天和最后一天
    :param date: 日期对象
    :return: (first_day, last_day)
    """
    first_day = date.replace(day=1)
    last_day = date.replace(day=calendar.monthrange(date.year, date.month)[1])
    return first_day, last_day

def get_dates(months_back=0):
    """
    获取从指定月份的第一天到当前月份的最后一天的日期范围
    :param months_back: 向前推的月数（非负整数）
    :return: [start_date, end_date]
    """
    if not isinstance(months_back, int) or months_back < 0:
        raise ValueError("months_back must be a non-negative integer")

    now = datetime.now()
    target_date = now.replace(day=1)
    for _ in range(months_back):
        target_date = (target_date - timedelta(days=1)).replace(day=1)
    first_day_of_target_month, _ = get_month_range(target_date)
    _, last_day_of_current_month = get_month_range(now)

    return [
        first_day_of_target_month.strftime("%Y-%m-%d"),
        last_day_of_current_month.strftime("%Y-%m-%d")
    ]


if __name__ == '__main__':
    dates = get_dates(1)
    print(dates)